package com.seaboxdata.controller;

import com.seaboxdata.service.StatisticsService;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @create 2021-01-04 10:18
 **/
@RestController
@EnableAutoConfiguration
@RequestMapping("/statistics")
public class StatisticsController {

    private static Map<String,Map<String,Object>> map;

    @Autowired
    private StatisticsService statisticsService;

    @RequestMapping(value = "/getStatisticInfo")
    public Map<String, Object> getStatisticsData(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String today = sdf.format(date);
        String yesterday = sdf.format(DateUtils.addDays(date, -1));
        if(map == null){
            map = new HashMap<>();
            map.put(today,statisticsService.getStatistics());
        }else{
            if(map.get(today) != null){
                if(map.get(yesterday) != null){
                    map.remove(yesterday);
                }
                return map.get(today);
            }else{
                return statisticsService.getStatistics();
            }
        }
        return statisticsService.getStatistics();
    }

    @RequestMapping("/info")
    public Map<String, Object> getData(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String today = sdf.format(date);
        String yesterday = sdf.format(DateUtils.addDays(date, -1));
        if(map == null){
            map = new HashMap<>();
            map.put(today,statisticsService.getStatistics());
        }else{
            if(map.get(today) != null){
                if(map.get(yesterday) != null){
                    map.remove(yesterday);
                }
                return map.get(today);
            }else{
                return statisticsService.getStatistics();
            }
        }
        return statisticsService.getStatistics();
    }
}
